import {getSignCode} from '../../../api/member'
Component({
data:{
  signUserList:[], //tab列表
  postData:{}, //请求体
  codeUrl:"" ,//签到二维码
  show:false
},
methods:{
  onLoad(){
   this.getMemberList()
   this.getSignCode()
  },
  
  //获取成员列表
  getMemberList(){
    const signUserList = getApp().getMemberList().map((val:{stuId:number,userName:string})=>{
      return {
        id:val.stuId,
        label:val.userName
      }
    })
    if(!signUserList || signUserList.length === 0){
      this.setData({
        show:true
      })
      return
    }
    const stuId = signUserList[0].id //学生id
    const userId = getApp().getUserInfo().userId;//用户id
    this.setData({
     signUserList,
     postData:{
      stuId,
      userId
     }
    })
  },
  // 获取签到二维码
  async getSignCode(){
  const postData = this.data.postData as any
  const {code,data:res} = await getSignCode(postData)
  if(code === 200){
    this.setData({
      codeUrl:res
    })
  }
  },
  // tab栏切换
  handleChange(e:any){
  const postData =this.data.postData as any
  postData.stuId = e.detail
  this.setData({
    postData
  })
  this.getSignCode()
  },
  // 跳转历史记录
  signInHistory(){
   wx.navigateTo({
     url:"/signIn_package/pages/signHistory/signHistory"
   })
  },

  // 分享二维码
  share() {
  // 去掉Base64前缀
  const base64Data = this.data.codeUrl
const base64Content = base64Data.replace(/^data:image\/\w+;base64,/, "");
// 将Base64转换为ArrayBuffer
const arrayBuffer = wx.base64ToArrayBuffer(base64Content);
// 获取文件系统管理器
const fs = wx.getFileSystemManager();
const filePath = `${wx.env.USER_DATA_PATH}/share_image.png`;  // 指定保存路径
// 将ArrayBuffer写入文件
fs.writeFile({
  filePath: filePath,
  data: arrayBuffer,
  encoding: 'binary',
  success: () => {
         wx.showShareImageMenu({
          path:filePath,
          success(res) {
            console.log('分享图片成功',res);
          },
          fail(err) {
            console.error('分享图片失败', err);
          }
    })
    // 文件保存成功后可以进行分享
  },
  fail: (err) => {
    console.error('文件保存失败', err);
  }
});


},
// 添加学员
add(){
  wx.navigateTo({
    url:"/curriculum_package/pages/addStudents/addStudents"
  })
},
cancel(){
  wx.navigateBack()
}
}
})